/*
 * @Description: 
 * @Author: paddies
 * @Date: 2024-06-08 22:34:23
 * @LastEditors: paddies
 * @LastEditTime: 2025-09-30 03:41:50
 * @FilePath: /shared_components/lib/luntai-components/open-tire-size-panel/openTireSizePanel.dart
 */
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:shared_components/build-button/buildButton.dart';
import 'package:shared_components/build-common/buildCommon.dart';
import 'package:shared_components/build-image/buildImage.dart';
import 'package:shared_components/build-input/buildInput.dart';
import 'package:shared_components/build-upload/buildUpload.dart';

openTireSize({
  String title = '确认完成',
  List<dynamic> goodsList = const [],
  int count = 1,
  dynamic item,
  double height = 600,
  Function? onSubmit,
}) {
  print("配件列表====");
  print(goodsList);
  var list = [].obs;
  for (var i = 0; i < count; i++) {
    list.value.add("");
  }

  var confirmPic = [].obs;
  //根据配件处理
  var newList = [].obs;
  for (var i = 0; i < goodsList.length; i++) {
    var data = goodsList[i] as Map<String, dynamic>;
    var n = int.parse(showText(data["quantity"]));
    var tire = [];
    for (var j = 0; j < n; j++) {
      tire.add("");
    }
    newList.value.add({...data, "tire": tire});
  }

  print("处理后的结果");
  print(newList);

  Get.bottomSheet(
      isScrollControlled: true,
      Container(
          padding: EdgeInsets.only(left: 10, right: 10, top: 20),
          decoration: new BoxDecoration(
            color: Colors.white,
            // new Radius.circular(20.0)
            borderRadius: new BorderRadius.only(
                topLeft: new Radius.circular(20.0),
                topRight: new Radius.circular(20.0)), //圆角
          ),
          height: height,
          child: Container(
            width: double.infinity,
            height: double.infinity,
            color: Colors.transparent,
            child: Column(
              children: [
                Container(
                  width: double.infinity,
                  alignment: Alignment.center,
                  child: Text(
                    title,
                    style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
                  ),
                ),
                Expanded(
                  child: Container(
                    width: double.infinity,
                    height: double.infinity,
                    color: Colors.white,
                    child: SingleChildScrollView(
                        child: Column(
                      children: [
                        //旧逻辑
                        Visibility(
                            visible: goodsList.isEmpty,
                            child: Column(
                              children: [
                                ...list.value.asMap().keys.map((i) {
                                  return buildInput(
                                      required: true,
                                      title: "胎码",
                                      placeholder: "请输入胎码",
                                      value: list.value[i],
                                      inputFormatters: [
                                        FilteringTextInputFormatter.deny(
                                            RegExp(',')),
                                        FilteringTextInputFormatter.deny(
                                            RegExp('，'))
                                      ],
                                      onChanged: (v) {
                                        list.value[i] = v;
                                      });
                                }).toList(),
                              ],
                            )),

                        //新逻辑
                        Visibility(
                            visible: goodsList.isNotEmpty,
                            child: Column(children: [
                              ...newList.value.asMap().keys.map((i) {
                                return Container(
                                  width: double.infinity,
                                  // height: 70,
                                  child: Column(
                                    children: [
                                      Container(
                                          width: double.infinity,
                                          height: 70,
                                          child: Row(
                                            children: [
                                              Container(
                                                width: 70,
                                                height: double.infinity,
                                                // color: Colors.yellow,
                                                child: buildImage(
                                                    type: "ClipRect",
                                                    url: showText(newList[i]
                                                        ["skuPicUrl"])),
                                              ),
                                              Expanded(
                                                  child: Container(
                                                width: double.infinity,
                                                height: double.infinity,
                                                padding: EdgeInsets.only(
                                                    left: 10, top: 10),
                                                // color: Colors.blue,
                                                child: Text(showText(
                                                    newList[i]["productName"])),
                                              )),
                                              Container(
                                                width: 70,
                                                height: double.infinity,
                                                padding: EdgeInsets.only(
                                                    top: 10, right: 10),
                                                alignment: Alignment.topRight,
                                                // color: Colors.green,
                                                child: Text(
                                                  "x" +
                                                      showText(newList[i]
                                                          ["quantity"]),
                                                  style: TextStyle(
                                                      color: Colors.red),
                                                ),
                                              )
                                            ],
                                          )),
                                      Container(
                                        width: double.infinity,
                                        child: Column(
                                          children: [
                                            ...(newList[i]["tire"] as List)
                                                .asMap()
                                                .keys
                                                .map((j) {
                                              return buildInput(
                                                  required: true,
                                                  title: "胎码",
                                                  placeholder: "请输入胎码",
                                                  value: newList.value[i]
                                                      ["tire"][j],
                                                  inputFormatters: [
                                                    FilteringTextInputFormatter
                                                        .deny(RegExp(',')),
                                                    FilteringTextInputFormatter
                                                        .deny(RegExp('，'))
                                                  ],
                                                  onChanged: (v) {
                                                    newList.value[i]["tire"]
                                                        [j] = v;
                                                    print("数据===");
                                                    print(newList.value);
                                                    // list.value[i] = v;
                                                  });
                                            }).toList(),
                                          ],
                                        ),
                                      )
                                    ],
                                  ),
                                  // Text(showText(newList[i]["productName"])),
                                  // color: Colors.red,
                                );
                              }).toList(),
                            ])),

                        Container(
                          width: double.infinity,
                          height: 40,
                          alignment: Alignment.centerLeft,
                          padding: const EdgeInsets.only(left: 20),
                          child: const Text("完成回执"),
                        ),
                        Obx(() => buildUpload(
                            list: confirmPic,
                            rowCount: 3,
                            max: 1,
                            // imageType: "BusinessLicense",
                            onOcrContent: (orcData) {
                              // controller.handleOrcData(
                              //     imageType: "BusinessLicense",
                              //     orcData: orcData);
                            },
                            onChange: (type, value, index) {
                              if (type == 'add') {
                                print("添加返回");
                                confirmPic.add(value);
                              }
                              if (type == 'del') {
                                confirmPic.removeAt(index);
                              }
                              if (type == 'change') {
                                confirmPic[index] = value;
                              }
                            })),

                        SizedBox(
                          height: 20,
                        ),
                        Container(
                            width: double.infinity,
                            height: 70,
                            padding:
                                EdgeInsets.only(left: 20, right: 20, top: 5),
                            // color: Colors.red,
                            child: buildButton(
                                title: "确定",
                                onClick: () async {
                                  //旧的逻辑
                                  if (goodsList.isEmpty) {
                                    var index = list.value
                                        .indexWhere((element) => element == "");
                                    if (index != -1) {
                                      EasyLoading.showToast("请输入胎码");
                                      return;
                                    }

                                    if (onSubmit != null) {
                                      onSubmit(list.join(","));
                                    }
                                  }

                                  //新的逻辑
                                  if (newList.isNotEmpty) {
                                    var index =
                                        newList.value.indexWhere((element) {
                                      var tire = element["tire"] as List;
                                      var i = tire.indexWhere((el) => el == "");
                                      print("里面===");
                                      print(i);

                                      return i != -1;
                                    });
                                    print(index);
                                    if (index != -1) {
                                      EasyLoading.showToast("请输入胎码");
                                      return;
                                    }

                                    if (onSubmit != null) {
                                      var res = [];
                                      //处理数据
                                      for (var k = 0;
                                          k < newList.value.length;
                                          k++) {
                                        var tire =
                                            newList.value[k]["tire"] as List;
                                        for (var h = 0; h < tire.length; h++) {
                                          res.add({
                                            "orderSn": newList[k]["orderSn"],
                                            "tire": tire[h],
                                            "confirmPic":
                                                confirmPic.value.join(","),
                                          });
                                        }
                                      }

                                      print("翻译后的数据===");
                                      print(res);

                                      onSubmit(res);
                                    }
                                  }
                                  Get.back();
                                },
                                colors: [Color(0xff0000FF), Color(0xff0000FF)]))
                      ],
                    )),
                  ),
                )
              ],
            ),
          )));
}
